<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${className}Mapper">
	<resultMap id="${className}Map" type="${modelPackage}.${className}">
		<%
			for(c in columnList){
		        print("\t\t<result column=\"");
		        print(c.columnName);
		        print("\" property=\"");
		        print(c.columnProp);
				print("\" jdbcType=\"");
				print(c.jdbcType);
		        print("\"/>");
		        println();
			}	
		%>
	</resultMap>
	
	<sql id="BASE_COLUMN_LIST">
		<%
			print("\t\t");
			for(c in columnList){
		        print(c.columnName);
		        if(!cLP.last){
		        	print(",");
		        }
			}
			println();
		%>
	</sql>
	
	<sql id="where">
		<%
			print("\t\t<where>");
			for(c in columnList){
			 	println();
				print("\t\t\t<if test=\"");
				print(c.columnProp);
				print(" != null");
				if(c.javaType=="String"){
					print(" and ");
					print(c.columnProp);
					print(" != ''");
				}
				print(" \">");
				println();
				print("\t\t\t\tand ");
				print(c.columnName);
				print(" = #{");
				print(c.columnProp);
				print("}");
				println();
				print("\t\t\t</if>");
			}
			println();
			print("\t\t</where>");
			println();
		%>
	</sql>
	
	<select id="selectByCondition" parameterType="${modelPackage}.${className}" resultMap="${className}Map">
		select <include refid="BASE_COLUMN_LIST"/> from ${tableName}
		<include refid="where"/>
		<include refid="CommonMapper.page" />
	</select>
	
	<update id="update" parameterType="${modelPackage}.${className}">
		update ${tableName}
		<set>
			<%
				for(c in columnList){
					if(cLP.first){
						continue;
					}
					print("\t\t\t<if test=\"");
					print(c.columnProp);
					print(" != null \">");
					println();
					print("\t\t\t\t");
					print(c.columnName);
					print(" = #{");
					print(c.columnProp);
					print(",jdbcType=");
					print(c.jdbcType);
					print("},");
					println();
					print("\t\t\t</if>");
					if(!cLP.last){
			        	println();
			        }
				}
				println();
			%>
		</set>
		where ${columnKeyName} = #{${columnKeyProp}};
	</update>
	
	<insert id="insert" parameterType="${modelPackage}.${className}">
		insert into ${tableName}
		<%
			print("\t\t(");
			for(c in columnList){
				if(!c.auto){
					print(c.columnName);
					if(!cLP.last){
			        	print(",");
			        }
				}
			}
			print(")");
			println();
			print("\t\tvalues(");
			for(c in columnList){
				if(!c.auto){
					println();
					print("\t\t");
					print("#{");
					print(c.columnProp);
					print(",jdbcType=");
					print(c.jdbcType);
					print("}");
					if(!cLP.last){
			        	print(",");
			        }
				}
		        
			}
			print(")");
			println();
		%>
	</insert>
</mapper>